<?php
/**
 * User: madis
 * Date: 26.09.2009
 * Time: 11:50:29
 */

class Lesson {

    private $info;
    private $numberOfWeekday;
    private $outcomeCache = '';
    private $weeks;
    //private $firstWeekOfAcademicYear = 35;

    public function __construct($lessonInfo, $numberOfWeekday) {
        $this->info = $lessonInfo;
        $this->numberOfWeekday = $numberOfWeekday;
        $this->makeEvent();
    }

    public function getContent() {
        return $this->outcomeCache;
    }

    private function makeEvent() {
        $this->weeks = $this->info['NADALAD'];
        if (strstr($this->weeks, '-')) { // If weeks are set as period A-B
            $this->addWeeklyEvent();
        } else if (strstr($this->weeks, ',')) { // If weeks are set as list A, B, C, ...
            $this->addMultipleEvents();
        } else { // If only one week is set
            $this->addOneEvent();
        }
    }

    private function addWeeklyEvent() {
        $firstAndLastWeek = explode('-', $this->weeks);
        $lastWeek = FIRST_WEEK_OF_ACADEMIC_YEAR+$firstAndLastWeek[1];
        $this->addEvent($firstAndLastWeek[0], $lastWeek);
    }

    private function addMultipleEvents() {
        $listOfWeeks = explode(', ', $this->weeks);
        foreach($listOfWeeks as $weekNumber) { // Add separate event for each week
            $this->addEvent($weekNumber);
        }
    }

    private function addOneEvent() {
        $this->addEvent($this->weeks);
    }

    private function addEvent($week, $lastWeek = 0) {
        $week += FIRST_WEEK_OF_ACADEMIC_YEAR;
        $vEvent = new VEvent($this->info, $week, $this->numberOfWeekday, $lastWeek);
        $this->outcomeCache .= $vEvent->getContent();
    }
}